;(function(window, $){
	var url = {
			addQrcode: './api/qrcode.php',
			getQrcodeList: './api/getQrcodeList.php'
		},
		$qrcodeAdd = $('#qrcode-add'),
		$qrcodeAddModal = $('#qrcode-add-modal'),
		listPage = 1,
		pageSize = 10;

	function init(){
		bind();
		getList();
	}

	function bind(){
		$qrcodeAdd.on('click', function (e) {
			e.preventDefault();
			console.log('555');
			var tmpl = $('#qrcode-add-tmpl').tmpl();
	        $qrcodeAddModal.html(tmpl).modal('show');
            console.log($qrcodeAddModal);
	    });

	    $qrcodeAddModal.on('submit', function (e) {
	    	e.preventDefault();
	    	var $this = $(this),
	    		$error = $('.error-tip.qrcode-name'),
	    		qrcodeId = $('#qrcode-id-input').val(),
	    		qrcodeName = $.trim($('#qrcode-name-input').val()),
	    		reqData = {
	    			channel: qrcodeName
	    		};

	    	if(!qrcodeName){
	    		$error.html('渠道名不能为空').removeClass('hidden');
	    		return;
	    	}
	    	if(km.getUTF8Length(qrcodeName) > 20){
	    		$error.html('渠道名不能超过10个汉字').removeClass('hidden');
	    		return;
	    	}

	    	if(qrcodeId){
	    		reqData.id = qrcodeId;
	    		reqData.action = 'edit';
	    		tipsMsg.tipsShow('正在修改，请稍候...');
	    	}else{
	    		reqData.action = 'add';
	    		tipsMsg.tipsShow('正在添加，请稍候...');
	    	}

			$.post(url.addQrcode, reqData).done(function(data){
				$qrcodeAddModal.modal('hide');
				tipsMsg.tipsHide();
				getList({ page: 1});
			});
	    });

	    $('.info-list').on('click', '.editIcon', function() {
			console.log('555');
	    	var data = $(this).parents('tr').tmplItem().data;
            var	tmpl = $('#qrcode-add-tmpl').tmpl(data);
	    	console.log(data);
	        $qrcodeAddModal.html(tmpl).modal('show');
	    });
	}

	function getList(options){
		tipsMsg.tipsShow();
		var param = $.extend({
			page: listPage,
			pageSize: pageSize
		}, options);
		$.get(url.getQrcodeList, param).done(function(data){
			var list = data.rst.data,
				tmpl;
			if(list.length){
				tmpl = $('#info-list-tmpl').tmpl(list);
			}else{
				tmpl = '<td class="ineffectiveness" colspan="5">暂无数据。</td>';
			}
			$('.info-list-count .number').text(data.rst.pageInfo.totalNum);
			$('.info-list tbody').html(tmpl);
			km.pageBar(data.rst.pageInfo, $('.info-list-pagination'), function (page) {
                getList({ page: page});
            });
			tipsMsg.tipsHide();
		});
	}

	init();

})(window, jQuery);